package com.csz.actions;

import com.csz.model.Usuario;
import java.util.Arrays;
import java.util.List;
import javax.servlet.RequestDispatcher;

/**
 *
 * @author Leonardo
 */
public class ActionLogin extends Action {

    private static final long serialVersionUID = 1L;

    @Override
    public void execute() {
        try {
            response.setContentType("text/html;charset=UTF-8");

            //Catando o login e a senha da tela
            String vlogin = request.getParameter("txtLogin");
            String vSenha = request.getParameter("txtSenha");

            //Verificando se o login e a senha batem
            List params = Arrays.asList(new Object[]{vlogin, vSenha});
            List<Object> lista = Usuario.consultarNamedQuery(Usuario.ConsultarPorLogin, params);

            //Se baterem continua, senao gera uma excessao
            if (lista.size() > 1 || lista.size() == 0) {
                throw new Exception("");
            }

            // Vai tartaruguinha
            RequestDispatcher rd = request.getRequestDispatcher("biotipo.jsp");
            rd.forward(request, response);

        } catch (Exception e) {
            request.setAttribute("erro", "login");
            request.setAttribute("exception", e.getMessage());
            RequestDispatcher rd = request.getRequestDispatcher("login.jsp");
            try {
                rd.forward(request, response);
            } catch (Exception ex) {
            }
        }
    }
}
